﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.Migrations;
using Microsoft.AspNet.Identity.EntityFramework;

namespace JustTest.Models
{
    public class ApplicationContextMigrations : DbMigrationsConfiguration<JustTestDb>
    {
        public ApplicationContextMigrations()
        {
            this.AutomaticMigrationDataLossAllowed = true;
            this.AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(JustTestDb context)
        {
            var user = context.Users.FirstOrDefault(u=>u.UserName == "pesho");

            if (user == null)
            {
                var adminRole = new Role
                {
                    Name = "Admin"
                };
                var authorRole = new Role
                {
                    Name = "Author"
                };
                context.Roles.Add(adminRole);
                context.Roles.Add(authorRole);
                context.SaveChanges();

                user = new ApplicationUser
                    {
                        FirstName = "pesho",
                        LastName = "peshev",
                        UserName = "pesho",
                    };

                context.Users.Add(user);
                context.SaveChanges();

                var category = new Category
                {
                    Name = "first category"
                };
                context.Categoriess.Add(category);
                context.SaveChanges();

                for (int i = 1; i < 3; i++)
                {
                    Test test = new Test
                    {
                        Title = "Seed Test #" + i,
                        Description = "some cool description #" + i,
                        User = user,
                        ReleaseDate = DateTime.Now,
                    };
                    test.Category = category;
                    context.Tests.Add(test);
                    context.SaveChanges();

                    for (int j = 1; j < 6; j++)
                    {
                        Question question = new Question
                        {
                            Content = "Question #" + j,
                            Number = j,
                            Test = test
                        };
                        context.Questions.Add(question);
                        context.SaveChanges();

                        for (int k = 1; k < 5; k++)
                        {
                            Answer answer = new Answer
                            {
                                Content = "Answer content #" + k,
                                Value = k * 5,
                                Question = question
                            };
                            context.Answers.Add(answer);
                            context.SaveChanges();
                        }
                    }
                }
            }

            base.Seed(context);
        }
    }
}